package logic

import (
	"context"
	"fmt"
	"zhihu-new/models/mysql"

	"zhihu-new/zhihurpc/internal/svc"
	"zhihu-new/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProblemLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddProblemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProblemLogic {
	return &AddProblemLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddProblemLogic) AddProblem(in *zhihurpc.AddProblemRequest) (*zhihurpc.AddProblemResponse, error) {
	// todo: add your logic here and delete this line
	var problem mysql.Problem
	problem = mysql.Problem{
		Title:   in.Title,
		Context: in.Context,
		UserId:  int(in.UserId),
	}
	err := mysql.DB.Debug().Create(&problem).Error
	if err != nil {
		return nil, fmt.Errorf("问题发布失败")
	}

	return &zhihurpc.AddProblemResponse{Id: int64(problem.ID)}, nil
}
